<?php
/**
 * @package		FVB_feed_manager
 * @subpackage	Helper
 * @author		yopensource
 * @copyright 	yopensource (yopensource@gmail.com)
 * @license		Commercial
 */
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();

/**
 * FVB Resource URL Processor
 */
class FVBResource_Url extends JObject 
{	
	var $str_in = '';
	var $str_page_url = '';
	
	function __construct($str_in, $str_page_url){
		//var_dump($str_in, $str_page_url);die();
		$this->str_in = $str_in;
		$this->str_page_url = $str_page_url;
	}
	
	function process_single_url(){
		return $this->_process_url($this->str_in);
	}
	
	function process_href(){
		//find all href value in a tags (without ")
		if(preg_match_all('/<a[^>]+(href=([^"].*?))(\s|>)/ism', $this->str_in, $matches)){
			for ($i = 0; $i < count($matches[1]); $i++){
				$this->str_in = str_replace($matches[1][$i], 'href="'. $matches[2][$i] .'"', $this->str_in);
			}
		}
		//find all href value in a tag (with ")
		if(preg_match_all('/<a[^>]+(href="([^"]+)")/sim', $this->str_in, $matches)){
			for ($i = 0; $i < count($matches[1]); $i++){
				$this->str_in = str_replace($matches[1][$i], 'href="'. $this->_process_url($matches[2][$i]) .'"', $this->str_in);
			}
		}
		
		return $this->str_in;
	}
	
	function process_src(){
		//find all href value in a tags (without ")
		if(preg_match_all('/<img[^>]+(src=([^"\'].*?))(\s|\/>|>)/ism', $this->str_in, $matches)){
			for ($i = 0; $i < count($matches[1]); $i++){
				$this->str_in = str_replace($matches[1][$i], 'src="'. $matches[2][$i] .'"', $this->str_in);
			}
		}
		//find all href value in a tag (with ')
		if(preg_match_all('/<img[^>]+(src=\'([^\']+)\')/ism', $this->str_in, $matches)){
			for ($i = 0; $i < count($matches[1]); $i++){
				$this->str_in = str_replace($matches[1][$i], 'src="'. $this->_process_url($matches[2][$i]) .'"', $this->str_in);
			}
		}
		//find all href value in a tag (with ")
		if(preg_match_all('/<img.*?(src="([^"]+)")/sim', $this->str_in, $matches)){
			for ($i = 0; $i < count($matches[1]); $i++){
				$this->str_in = str_replace($matches[1][$i], 'src="'. $this->_process_url($matches[2][$i]) .'"', $this->str_in);
			}
		}
		
		return $this->str_in;
	}
	
	private function _process_url($str_value){
		if (preg_match('/^http/i', $str_value)) {
			return $str_value;
		}
		if (preg_match('/^\//i', $str_value)) {
			return $this->_get_root_url().$str_value;
		}
		return $this->_get_path_url().$str_value;
	}
	
	private function _get_root_url(){
		$referer = parse_url($this->str_page_url);
		$referer['path'] = null;
		$referer['query'] = null;
		$referer['fragment'] = null;
		return $this->_http_build_url($referer);
	}
	
	private function _get_path_url(){
		$referer = parse_url($this->str_page_url);
		$referer['path'] = preg_replace('/\/[^\/]*$/', '/', $referer['path']);
		$referer['query'] = null;
		$referer['fragment'] = null;
		return $this->_http_build_url($referer);
	}
	
	private function _http_build_url($referer){
		$str_return 	= $referer['scheme'];
		$str_return 	.= '://';
				
		$str_return 	.= isset($referer['user']) ? $referer['user'] : '';
		$str_return 	.= isset($referer['pass']) ? ':'.$referer['pass'] : '';
		
		$str_return 	.= isset($referer['port']) ? ':'.$referer['port'] : '';
		$str_return 	.= $referer['host'];
		$str_return 	.= $referer['path'];
		
		$str_return 	.= isset($referer['query']) ? '?'.$referer['query'] : '';	
		$str_return 	.= isset($referer['fragment']) ? '#'.$referer['fragment'] : '';	
		
		return $str_return;
	}
}